﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ParticleFiltersDemo1
{
    public class Particle : ViewModelBase
    {
        private double x;
        private double y;
        public double heading;
        public double weight;
        bool isNoisy; // дали има шум - рандом - AddNoise() - за да работи по-добре
        Board board; // картата

        public double X
        {
            get
            {
                return this.x;
            }
            set
            {
                if (this.x == value)
                    return;

                this.x = value;
                this.NotifyPropertyChanged("X");
            }
        }

        public double Y
        {
            get
            {
                return this.y;
            }
            set
            {
                if (this.y == value)
                    return;

                this.y = value;
                this.NotifyPropertyChanged("Y");
            }
        }

        public Action PositionChanged;

        public Particle( double x, double y, double heading, double weight, bool isNoisy, Board board )
        {
            this.x = x;
            this.y = y;
            this.heading = heading;
            this.weight = weight;
            this.isNoisy = isNoisy;
            if ( this.isNoisy )
            {
                AddNoise();

            }
            this.board = board;

        }
        //if we do not know the heading
        public Particle( double x, double y, double weight, bool isNoisy, Board board )
            : this( x, y, -1, weight, isNoisy, board )
        {
            this.heading = 0;
        }

        public bool IsNextToObstacle
        {
            get;
            set;
        }

        public void AddNoise()
        {
            Random r = new Random();
            this.x += ( double )r.Next( -1, 1 ) / 10;
            this.y += ( double )r.Next( -1, 1 ) / 10;
            this.heading += ( double )r.Next( -1, 1 ) / 10;

        }

        public void SetHeading( double h )
        {
            heading = h;
        }

        /*
         * Мести партикъл
         */ 
        public bool AdvanceBy( double speed, bool noisy )
        {
            if ( noisy )
            {
                Random r = new Random();
                heading += r.Next( -3, 3 );
            }

            double rad = heading * Math.PI / 180;
            double dx = Math.Sin( rad ) * speed;
            double dy = Math.Cos( rad ) * speed;
            if ( board.IsEmpty( ( int )( x + dx ), ( int )( x + dy ) ) )
            {
                x += dx;
                y += dy;
                return true;
            }

            return false;
        }

    }
}
